<?php
/**
 * 线下活动
 * @author jianhui
 *
 */
class SpecialPOffline extends MY_Controller{
    function SpecialPOffline(){
        parent::MY_Controller();
        //设置导航选中
        $this->navbarFocus = 'SpecialBudget';
    }
    /**
     * 添加线下活动
     */
    function add(){
        $this->load->model('specialPOfflineModel');
        if ($_POST){
            $offlineId = (int)$this->input->post('id');
            $data = array(
                'storeId'   => $this->user['userId'],
                'procId'    => (int)$this->input->post('procId'),
                'projectId' => (int)$this->input->post('projectId'),
                'address'   => $this->input->post('address'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'remarks'   => $this->input->post('remarks'),
            );
            $data['procId'] || showError('请选择主线流程');
            $data['projectId'] || showError('请选择项目');
            $data['address'] || showError('请输入活动地址');
            $data['startTime'] || showError('请选择活动开始时间');
            $data['endTime'] || showError('请选择活动结束时间');
            if ($offlineId){
                $offlineId = $this->specialPOfflineModel->edit($offlineId, $data);
            }else{
                $data['createTime'] = time();
                $offlineId = $this->specialPOfflineModel->add($data);
            }
            if ($offlineId){
                $params = array(
                    'openUrl' => printUrl('SpecialPOffline', 'detailAdd', array('procId'=>$data['procId'], 'projectId'=>$data['projectId'],'offlineId'=>$offlineId)),
                    'id' => $offlineId,
                );
                showSuccess('保存成功', printUrl('SpecialPOffline', 'add', array('procId'=>$data['procId'], 'projectId'=>$data['projectId'],'offlineId'=>$offlineId)), 0, $params);
            }else{
                showError('保存失败');
            }
        }else{
            $offlineId = (int)$this->input->get('offlineId');
            $procId = (int)$this->input->get('procId');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $this->viewData['projectId'] = $projectId;
            if ($offlineId){
                $offlineData = $this->specialPOfflineModel->getData(array('id'=>$offlineId, 'storeId'=>$this->user['userId']));
                $offlineData || showError('您要修改的线下活动不存在');
                in_array($offlineData['budgetAuditRs'],array(-1,0,3)) || showError('此线下活动已审核，不能编辑');
                $this->viewData['offlineData'] = $offlineData;
                $this->viewData['procId'] = $offlineData['procId'];
                $this->viewData['projectId'] = $offlineData['projectId'];
                $this->load->model('specialPODetailModel');
                $this->viewData['offlineDetailList'] = $this->specialPODetailModel->getNewList(array('offlineId'=>$offlineId, 'storeId'=>$this->user['userId']));
            }
            $this->viewData['procId'] || showError('请选择主线流程');
            $this->viewData['projectId'] || showError('请选择项目ID');
            //主线流程数据
            $this->load->model('specialProcessModel');
            $procData = $this->specialProcessModel->getData($this->viewData['procId']);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            //是否为特殊补报
            $this->viewData['isSpecialBubao'] = $this->auth->isSpecialBudgetEdit($procData);
            //项目数据
            $this->load->model('specialProjectModel');
            $projectData = $this->specialProjectModel->getData($this->viewData['projectId']);
            $projectData || showError('项目数据不存在');
            $this->viewData['projectData'] = $projectData;
            $this->load->view('SpecialPOffline/add', $this->viewData);
        }
    }
    /**
     * 删除线下活动
     */
    function del(){
        $id = (int)$this->input->post('offlineId');
        $id || showError('请选择要删除的线下活动');
        $this->load->model('specialPOfflineModel');
        if ($id){
            $this->load->model('specialPODetailModel');
            $this->specialPODetailModel->del(array('offlineId'=>$id,'storeId'=>$this->user['userId']),'*');
            $this->specialPOfflineModel->del(array('id'=>$id,'storeId'=>$this->user['userId']));
            showSuccess('删除线下活动成功');
        }else{
            showError('删除线下活动失败');
        }
    }
    /**
     * 查看线下活动明细
     */
    function detailCheck(){
        $offlineId = (int)$this->input->get('offlineId');
        $offlineId || showError('请选择要查看的线下活动');
        $this->load->model('specialPODetailModel');
        $this->viewData['offlineDetailList'] = $this->specialPODetailModel->getNewList(array('offlineId'=>$offlineId));
        $this->load->view('SpecialPOffline/detailCheck', $this->viewData);
    }
    /**
     * 添加细项
     */
    function detailAdd(){
        $this->load->model('specialPODetailModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'   => $this->user['userId'],
                'procId'   => (int)$this->input->post('procId'),
                'projectId' => (int)$this->input->post('projectId'),
                'offlineId' => (int)$this->input->post('offlineId'),
                'type'      => (int)$this->input->post('type'),
                'descType'  => (int)$this->input->post('descType'),
                'descTypeAlias' => $this->input->post('descTypeAlias'),
                'startTime' => $this->input->post('startTime'),
                'endTime'   => $this->input->post('endTime'),
                'remarks'   => $this->input->post('remarks'),
                'budgetApply' => (float)$this->input->post('budgetApply')
            );
            $data['procId'] || showError('请选择主线流程');
            $data['projectId'] || showError('请选择项目');
            $data['offlineId'] || showError('请选择下下活动');
            $data['type'] || showError('明细类别不能为空');
            $data['descType'] || showError('请选择费用项目描述');
            if ($data['descType'] == -1 && !$data['descTypeAlias']){
                showError('请输入其它项目费用描述');
            }
            $data['startTime'] || showError('请选择活动开始时间');
            $data['endTime'] || showError('请选择活动结束时间');
            $data['budgetApply'] || showError('请填写预算费用');
            if ($id){
                $id = $this->specialPODetailModel->edit($id, $data);
            }else{
                $data['createTime'] = time();
                $id = $this->specialPODetailModel->add($data);
            }
            if ($id){
                showSuccess('保存成功', printUrl('SpecialPOffline', 'add', array('offlineId'=>$data['offlineId'])));
            }else{
                showError('保存错误');
            }
        }else{
            $id = (int)$this->input->get('id');
            $procId = (int)$this->input->get('procId');
            $this->viewData['procId'] = $procId;
            $projectId = (int)$this->input->get('projectId');
            $this->viewData['projectId'] = $projectId;
            $offlineId = (int)$this->input->get('offlineId');
            $this->viewData['offlineId'] = $offlineId;
            if ($id){
                $detailData = $this->specialPODetailModel->getData(array('id'=>$id, 'storeId'=>$this->user['userId']));
                $detailData || showError('线下活动明细数据不存在');
                $this->viewData['detailData'] = $detailData;
                $this->viewData['procId'] = $detailData['procId'];
                $this->viewData['projectId'] = $detailData['projectId'];
                $this->viewData['offlineId'] = $detailData['offlineId'];
            }
            $this->viewData['procId'] || showError('请选择主线流程');
            $this->viewData['projectId'] || showError('请选择项目');
            $this->viewData['offlineId'] || showError('请选择线下活动');
            //项目数据
            $this->load->model('specialProjectModel');
            $projectData = $this->specialProjectModel->getData($this->viewData['projectId']);
            $projectData || showError('项目数据不存在');
            $this->viewData['projectData'] = $projectData;
            //线下活动数据
            $this->load->model('specialPOfflineModel');
            $offlineData = $this->specialPOfflineModel->getData($this->viewData['offlineId']);
            $offlineData || showError('线下活动数据不存在');
            $this->viewData['offlineData'] = $offlineData;
            $this->load->view('SpecialPOffline/detailAdd', $this->viewData);
        }
    }
    /**
     * 删除下下活动明细
     */
    function detailDel(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的线下活动明细项');
        $this->load->model('specialPODetailModel');
        if ($this->specialPODetailModel->del(array('id'=>$id,'storeId'=>$this->user['userId']))){
            showSuccess('删除线下活动明细项成功');
        }else{
            showError('删除线下活动明细项失败');
        }
    }
}